<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./swiper/swiper.css">
  <style>
   
    .swiper1{
      width: 800px;
      height: 400px;
      margin: 10px auto;
      overflow-x: hidden;
    }
    .swiper1 .swiper-slide img{
      width: 800px;
      height: 400px;
    }
    
  </style>
</head>
<body>
  <div id="app">
    <common-head></common-head>
  </div>
  <script src="./vue.js"></script>
  <script src="./axios.js"></script>
  <script src="./swiper/swiper.js"></script>
  <script>
    const CommonHead = {
      template: `
        <div>
          <div class="swiper swiper1">
            <div class="swiper-wrapper">
              <div class="swiper-slide" v-for="banner in banners" :key="banner.id">
                <img :src="banner.picUrl"/>  
              </div>
            </div>
          </div>
        </div>
      `,
      data(){
        return {
          swiper: null,
          banners: []
        }
      },
      methods: {
        fetchBanners(){
          // 请求banner的数据
          axios.get('https://api.it120.cc/conner/banner/list').then(res => {
            if(res.data.code === 0) {
              this.banners = res.data.data

            }
          })
        },
        initSwiper(){
          // 初始化swiper
           this.swiper = new Swiper('.swiper1', {
            autoplay: true,//可选选项，自动滑动
          })
        }
      },
      watch: {
        banners(){
          // 数据过来重新初始化swiper
          this.$nextTick(() => {
            this.swiper.update();
          })
        }
      },
      created () {
        this.fetchBanners();
      },
      mounted () {
        this.initSwiper();
      }
    }
    Vue.component('CommonHead', CommonHead)
    const vm = new Vue({
      el: '#app',
    })


  </script>
</body>
</html>